var parentMenuTree, lookAuthorityMenuTree, operateAuthorityMenuTree;
var loadDiv = null;
$(function () {
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中

    initUpdateWindow();
});

/**
 * 初始化更新窗口
 * @returns {Function}
 */
function initUpdateWindow(){
	//获取父菜单树
	parentMenuTree = $.fn.zTree.getZTreeObj('parentMenuTree');
	//添加父菜单树的回调事件
	parentMenuTree.setting.callback.onClick = onDialogTreeOnClick("menuParentNameEdit","parentIdEdit");
	//获取查看权限树
	lookAuthorityMenuTree = $.fn.zTree.getZTreeObj('lookAuthorityMenuTree');
	//添加查看权限树的回调事件
	lookAuthorityMenuTree.setting.callback.onClick = onDialogTreeOnClick("menuLookAuthorityEdit","menuLookAuthorityIdEdit");
	//获取操作权限树
	post(getPath() + "base/authority/tree",{pid:0},function(treeNodes){
		treeNodes.splice(0,1);//去掉第一个节点
		$.fn.zTree.init($("#operateAuthorityTreeEdit"),operateAuthority_setting,treeNodes); 
		operateAuthorityMenuTree = $.fn.zTree.getZTreeObj('operateAuthorityTreeEdit');
		//取消所有已勾选的节点
		operateAuthorityMenuTree.checkAllNodes(false);
		var authorityIds = $("#operateAuthorityIdEdit").val();
		var authorityIdsArr = [];
		if(authorityIds){
			authorityIdsArr = authorityIds.split(",");
		}
		var node = null;
		for(var i=0; i<authorityIdsArr.length; i++) {
			node = operateAuthorityMenuTree.getNodeByParam("id", authorityIdsArr[i]);
			if(node.isParent){
				operateAuthorityMenuTree.expandNode(node, true, true, false);
			}else{
				var pNode = node.getParentNode();
				operateAuthorityMenuTree.expandNode(pNode, true, true, false);
			}
			operateAuthorityMenuTree.checkNode(node, true, false);
		}
		//添加操作权限树的回调事件
		operateAuthorityMenuTree.setting.callback.onCheck = onZTreeCheck("operateAuthorityEdit","operateAuthorityIdEdit");
	});
	//初始化点击事件
	initClick();
	//保存修改后的菜单
	saveMenu();
}

//初始化点击事件
function initClick(){
	//点击菜单所属父类
	$("#menuParentNameEdit").bind("click", {menuContentId:"parentMenu"}, showMenu).bind("focus", function(){
		parentMenuTree.expandNode(parentMenuTree.getNodeByTId("1"), true);
		//取消所有已勾选的节点
		parentMenuTree.checkAllNodes(false);
		var parentMenuId = $("#parentIdEdit").val();
		var parentMenuNode = parentMenuTree.getNodeByParam("id", parentMenuId);
		parentMenuTree.selectNode(parentMenuNode, false);
	});
	//点击查看权限
	$("#menuLookAuthorityEdit").bind("click", {menuContentId:"lookAuthorityMenu"}, showMenu).bind("focus", function(){
		lookAuthorityMenuTree.expandNode(lookAuthorityMenuTree.getNodeByTId("1"), true);
		//取消所有已勾选的节点
		lookAuthorityMenuTree.checkAllNodes(false);
		//获取查看权限的id值
		var menuLookAuthorityId = $("#menuLookAuthorityIdEdit").val();
		var menuLookAuthorityNode = lookAuthorityMenuTree.getNodeByParam("id", menuLookAuthorityId);
		lookAuthorityMenuTree.selectNode(menuLookAuthorityNode, true, false);
	});
	
}

//保存修改后的菜单
function saveMenu(){
	//声明消息提示
    var toolTips = new Object();
    //设置表单验证
    var validator = $("#updateForm").validate({
    	rules:{
        	label:"required",
        	menuParentName:"required",
        //	className:"required",
        	controlHeight:"required",
        	sortFlag:"required",
        	menuLookAuthorityEdit:"required"
        },
        messages:{
        	label:"请输入菜单名称",
        	menuParentName:"请选择所属父类",
        	className:"请输入菜单样式",
        	controlHeight:"请输入菜单高度",
        	sortFlag:"请输入菜单序号",
        	menuLookAuthorityEdit:"请选择菜单查看权限"
        },
        errorPlacement: function(eror,element){
            var toolTip = createTooltip(toolTips,element.attr("id"));
            toolTip.setContent(eror.text());
            element.addClass("error-border");

        },
        success:function(label){
            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
            var toolTip = toolTips[elementId];
            $("#" + elementId).removeClass("error-border");
            toolTip.close();
            toolTip.detach();
        },
        submitHandler:function(form){
        	var parentNode = getNodeById('parentMenuTree', $("#parentIdEdit").val());
        	var pNode=null;
        	if(parentNode){
        		pNode = parentNode.getParentNode();
        	}
            if($("#parentIdEdit").val() == $("#updateId").val()){
                createWindow('所属父菜单不能是当前菜单!').dialog('open');
                return;
            }else if(pNode){
            	if(pNode.id==$("#updateId").val()){
        			createWindow('父节点不能是子节点').dialog('open');
                    return;
        		}
            }
        	var selectNodeArr = menuzTree.getSelectedNodes();
        	var lastNodeIndex = selectNodeArr.length - 1;
        	var menuId = selectNodeArr[lastNodeIndex].id; //取最后菜单节点的id
        	$("#updateId").val(menuId);
        	loadDiv.showLoading();
        	post(getPath() + "base/menu/edit/" + menuId,$("#updateForm").serialize(),function(resultObject){
        		loadDiv.hideLoading();
        		var errors = resultObject.errors;
                //判断后台是否有返回错误消息
                if(errors && errors.length > 0){
                    for(var i=0; i < errors.length; i++){
                        var error = errors[i];
                        var toolTip = createTooltip(toolTips,error.field);
                        toolTip.setContent(error.defaultMessage);
                        $("#" + error.field).addClass("error-border");
                    }
                }else{
                    //直接更改和移动弹出框的树形数据
                    var node1 = getNodeById('parentMenuTree', $("#updateId").val());
                    var parentNode = getNodeById('parentMenuTree', $("#parentIdEdit").val());
                    node1.name = $("#labelEdit").val();
                    var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
                    zTree.updateNode(node1);
                    var mo = zTree.moveNode(parentNode,node1,'inner');
                    //直接更改和移动左边的树形数据
                    var node2 = getNodeById('menuTree', $("#updateId").val());
                    var menuparentNode = getNodeById('menuTree', $("#parentIdEdit").val());
                    node2.name = $("#labelEdit").val();
                    var zTree = $.fn.zTree.getZTreeObj('menuTree');
                    zTree.updateNode(node2);
                    mo = zTree.moveNode(menuparentNode,node2,'inner');
                    createWindow("更新成功").dialog("open");
                }
        	},function(resultObject){
        		loadDiv.hideLoading();
        		createWindow(resultObject).dialog('open');
        	});
       
        }
    })
}

